
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/context.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <ostream>
#include <string>
#include <cstring>
#include <boost/asio/ts/internet.hpp>



int main(int argc, const char * argv[])
{
	try
	{
		if (argc != 3)
		{
			printf("Usage: http_client <server> <path>.\n");
			printf("example:\n");
			printf("\thttp_client www.boost.org /LICENSE_1_0.txt.\n");
			return 1;
		}
		// boost::asio::ssl::stream<boost::asio::ip::tcp::socket> m();
		boost::asio::ip::tcp::iostream s;

		s.expires_after(std::chrono::seconds(60));
		s.connect(argv[1], "https");

		if (!s)
		{
			printf("Unable to connect: %s.\n", s.error().message().c_str());
			return 1;
		}
		s << "GET " << argv[2] << " HTTP/1.0\r\n";
		s << "Host: " << argv[1] << "\r\n";
		s << "Accept: */*\r\n";
		s << "Connection: close\r\n\r\n";

		std::string http_version;
		s >> http_version;

		unsigned int status_code = 0;
		s >> status_code;

		std::string status_message;
		std::getline(s, status_message);
		if (!s || http_version.substr(0,5) != "HTTP/")
		{
			printf("Invalid response.\n");
			return 1;
		}
		if (status_code != 200)
		{
			std::cout << "response returned with status code : " << status_code << "\n";
			return 1;
		}
		std::string header;
		while (std::getline(s, header) && header!="\r") 
		{
			std::cout << header << "\n";
		}
		std::cout << "\n";

		std::cout << s.rdbuf();

	}
	catch(std::exception & e)
	{
		printf("exception : %s.\n", e.what());
	}
	return EXIT_SUCCESS;
}		